/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Dec 21, 2012  10:04:08 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.mail.sending
 * File Name : SendMailTLS.java
 * TODO
 */
package com.tis.mail.sending;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import promis.utility.CommonService;
import com.tis.mail.common.CommonMail;
/**
 * @author chatree
 *
 */
public class SendMailTLS extends CommonMail {

    static Properties mailServerProperties;
    static Session getMailSession;
    static MimeMessage generateMailMessage;
 
    public static void main(String args[]) throws AddressException, MessagingException {
        generateAndSendEmail("");
        System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
    }
    
    
    public static void generateAndSendEmail(String message) throws AddressException, MessagingException {
 
//Step1
        System.out.println("\n 1st ===> setup Mail Server Properties..");
        mailServerProperties = System.getProperties();
        mailServerProperties.put("mail.smtp.port", "25");
        mailServerProperties.put("mail.smtp.auth", "true"); 
        
        //mailServerProperties.put("mail.debug", "true");
        //for gmail
        //mailServerProperties.put("mail.smtp.starttls.enable", "true");
        System.out.println("Mail Server Properties have been setup successfully..");
 
//Step2
        System.out.println("\n\n 2nd ===> get Mail Session..");
        getMailSession = Session.getDefaultInstance(mailServerProperties, null);
        generateMailMessage = new MimeMessage(getMailSession);
        generateMailMessage.setFrom((new InternetAddress("noreply@tis.co.th")));
        generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("support@tis.co.th"));
        generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("chatree@tis.co.th"));
        generateMailMessage.setSubject("System Alter AutoChecked Interface Backlog at " + getDateTime(new Date()));
        
        String emailBody = CommonService.IsNullRString(message) + " " + "<br><br><br><br> Regards, <br>This is email gennerate from PROMIS Interface System Check.";
        
        
        generateMailMessage.setContent(emailBody, "text/html");
        System.out.println("Mail Session has been created successfully..");
 
//Step3        
        System.out.println("\n\n 3rd ===> Get Session and Send mail");
        Transport transport = getMailSession.getTransport("smtp");
        
        // Enter your correct gmail UserID and Password
        transport.connect("mail.tis.co.th", "noreply@tis.co.th", "tistis");
        transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
        transport.close();
        
    }
}
